package cn.lby.tp.downloadtest.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import butterknife.BindView;
import cn.lby.tp.downloadtest.BaseApplication;
import cn.lby.tp.downloadtest.R;
import down.DownLoaderHelper;
import down.DownLoaderManger;
import download_temp.FileInfo;

public class AddTaskActivity extends BaseApplication {

    @BindView(R.id.file_name)
    EditText fileName;

    @BindView(R.id.download_url)
    EditText downUrl;

    private DownLoaderManger downLoaderManger;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        downLoaderManger = DownLoaderManger.getInstance(new DownLoaderHelper(context));
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_add_task;
    }

    public void addTask(View view) {
        String name = fileName.getText().toString();
        String url = downUrl.getText().toString();

        if (!"".equals(name) && !"".equals(url)) {
            FileInfo info = new FileInfo(name, url);
            info.setLength(0);
            info.setFinished(0);
            downLoaderManger.addTask(info);
            Toast.makeText(this, "任务添加成功", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this, "必填项不能为空", Toast.LENGTH_SHORT).show();
        }
    }

    public void toDownList(View view) {
        Intent intent = new Intent(this, OkDownLoadingActivity.class);
        startActivity(intent);
    }
}